package com.jd.fruit.model;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.JsonConvertUtil;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 栏目
 * 
 */
@SuppressWarnings("serial")
@Table("t_chnl")
public class Chnl  extends BaseModel<Chnl> { 
	public static final Chnl dao = new Chnl();
	
	
	/**
	 * 保存栏目
	 * @param chnl
	 */
	public void save(Chnl chnl){
		try {
			chnl.set("id",StringUtil.getKey());
			chnl.save();
		} catch (Exception e) {
			throw new RuntimeException("栏目保存异常！", e);
		}
	}
	
	/**
	 * 首页栏目显示
	 */
	public static List<Chnl> indexList(){
      return  Chnl.dao.find("SELECT id,name,parent_id,url FROM t_chnl WHERE is_display=1");
	}
	/**
	 * 栏目顶级栏目列表
	 */
	public static List<Chnl> topList(){
      return  Chnl.dao.find("SELECT * FROM t_chnl WHERE is_display=1 and nav_is_display=1 and parent_id IS NULL");
	}
	/**
	 * 栏目顶级栏目列表
	 */
	public static List<Chnl> mangeTopList(){
      return  Chnl.dao.find("SELECT * FROM t_chnl WHERE parent_id IS NULL");
	}
	  
	/**
	 * 栏目子栏目列表
	 */
	public  List<Record> childList(String parentid){
      return  Db.find("SELECT * FROM t_chnl  WHERE  parent_id=? order by priority asc",parentid);
	}
	/**
	 * 标题子级栏目列表
	 */
	public  static List<Chnl> childList(){
      return  Chnl.dao.find("SELECT * FROM t_chnl WHERE is_display=1 and nav_is_display=1 and parent_id is not null");
	}
	
	/**
	 * 是否包含子栏目
	 * @param id
	 * @return
	 */
	public static boolean isHasChild(String parentid){
		long returnNum=Db.queryLong("SELECT COUNT(id) FROM t_chnl  WHERE  parent_id=?",parentid);
		return returnNum>0?true:false;
	}
	/**
	 * 读取栏目tree
	 * @param type
	 * @return
	 */
	public JSONArray loadTree(){
		String sql="SELECT * FROM `t_chnl` order by is_display desc,priority asc";
		List<Record> recordList=Db.find(sql);
		//预留角色map，在以后所属栏目默认选中
		String rolemap="";
	 
	    if(recordList.size()>0){
	    	   return JsonConvertUtil.tranformToJSON(recordList, rolemap);
		 }
	    //默认数据
	    JSONArray defaultArray=new JSONArray();
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "no have data!");
		defaultArray.add(defaultResult);
		return defaultArray;
	} 
	
	/**
	 * 读取栏目tree
	 * @param type 
	 * @return 
	 */
	public String loadTreeGrid(){
		String sql="SELECT * FROM `t_chnl` order by is_display desc,priority asc";
		List<Record> recordList=Db.find(sql);
		//预留角色map，在以后所属栏目默认选中
		if(recordList.size()>0){
	       return JsonConvertUtil.getFormatData(recordList);
		}
		 //默认数据
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "no have data!");
		return defaultResult.toJSONString();
	} 
	
	@Override
	public void initWhere() {
		if(StrKit.notBlank(this.getStr("id"))){
			appendSql("AND", "id", "=", getStr("id"));
		}
	}
	
}
